// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import FastClick from 'fastclick'
import VueRouter from 'vue-router'
import router from './router/index'
import App from './App'
import tools from './utils/tools'
import store from './store'
import 'babel-polyfill'
import Es6Promise from 'es6-promise'
require('es6-promise').polyfill()
Es6Promise.polyfill()

// import weui from 'weui.js'
// import 'weui'
//
// Vue.prototype.$weui = weui

Vue.prototype.$tools = tools;
Vue.use(VueRouter)

import {AjaxPlugin, LoadingPlugin, ToastPlugin, TransferDom, Confirm} from 'vux'
/*
* vux公用ui组件
* */
Vue.use(AjaxPlugin)
Vue.use(LoadingPlugin)
Vue.use(ToastPlugin)
Vue.component('confirm', Confirm)
Vue.directive('transfer-dom', TransferDom)

/*注册公共组件*/
import header from "./components/header" //头部
import footer from "./components/footer" //图标

Vue.component('common-header', header)
Vue.component('common-footer', footer)

FastClick.attach(document.body)

Vue.config.productionTip = false

// http 拦截器
Vue.http.interceptors.request.use(config => {
    // console.log(config)
    // form 提交方式
    config.headers['Content-Type'] = 'application/json'
    return config
}, err => {
    return Promise.reject(err)
})

router.beforeEach((to, from, next) => {
    if (from.name === 'null' || from.name === null ) {
        if(window.sessionStorage.getItem('historyPathList')){
            store.dispatch('changeHistoryPathList',JSON.parse(window.sessionStorage.getItem('historyPathList')))
        }

    }
    next()
})

/*
* 为部分接口添加操作提示
* */

Vue.http.interceptors.response.use(function (response) {
    Vue.$vux.loading.hide()
    console.log(response)
    let _url = response.config.url;
    let _index = _url.indexOf(':7001')
    let _index1 = _url.indexOf('?')
    let index = _index + 5
    let url = _url.slice(index + 1, _index1)

    if (response.data.code === '110') {
        tools.goPage('login')
    } else if (response.data.code === '0') {
        console.log(url)
        if (url === 'user/logout' || url === 'user/login' || url === 'user/modifypwd' || url === 'resident/deletePopulationInfo' || url === 'resident/savePopulationBaseInfo' || url === 'resident/updatePopulationOtherInfo' || url === 'resident/updateDangerPopulationInfo' || url === 'resident/saveIssueInfo' || url === 'resident/saveVisitedInfo' || url === 'resident/deleteVisitedInfo' || url === 'subsidyinfo/addSubsidyinfo' || url === 'subsidyinfo/editSubsidyinfo' || url === 'subsidyinfo/deleteSubsidyinfo' || url === 'resident/updateFamilyBaseInfo' || url === 'resident/addFamilyBaseInfo' || url === 'resident/updateFamilyWaterElectricInfo' || url === 'resident/saveLandTransferInfo' || url === 'resident/deleteLandTransferInfo' || url === 'resident/saveRefarmingInfo' || url === 'resident/deleteRefarmingInfo' || url === 'resident/saveHouseBaseInfo' || url === 'resident/deleteHouseBaseInfo' || url === 'resident/editchanye' || url === 'enterprise/modify' || url === 'enterprise/editProduct' || url === 'enterprise/delProduct'|| url === 'resident/updateFamilyGardenInfo') {
            Vue.$vux.toast.show({
                text: response.data.message,
                type: 'text',
                position: 'middle',
            })
            return response
        } else {
            return response
        }
    } else {
        Vue.$vux.toast.show({
            text: response.data.message,
            type: 'text',
            position: 'middle',
        })
        return response
    }

}, function (error) {
    Vue.$vux.loading.hide()
    Vue.$vux.toast.show({
        text: '请求失败，请稍后再试',
        position: 'middle',
        type: 'text'
    })
    return Promise.reject(error)
})

/* eslint-disable no-new */
new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app-box')
